<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../vue.js"></script>
</head>
<body>
    <div id="app">
        <table border="1">
            <thead>
                <tr>
                    <th colspan="4">购物车</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>名称</td>
                    <td>单价</td>
                    <td>数量</td>
                    <td>小计</td>
                </tr>
                <tr>
                    <td>{{cart[0].name}}</td>
                    <td>{{cart[0].price}}</td>
                    <td><input type="text" v-model="cart[0].num"></td>
                    <td>{{cart[0].num * cart[0].price}}</td>
                </tr>
                <tr>
                    <td>{{cart[1].name}}</td>
                    <td>{{cart[1].price}}</td>
                    <td><input type="text" v-model="cart[1].num"></td>
                    <td>{{cart[1].num * cart[1].price}}</td>
                </tr>
                <tr>
                    <td>{{cart[2].name}}</td>
                    <td>{{cart[2].price}}</td>
                    <td><input type="text" v-model="cart[2].num"></td>
                    <td>{{cart[2].num * cart[2].price}}</td>
                </tr>
                <tr>
                    <td colspan="2">总计</td>
                    <td>{{totalNum}}</td>
                    <td>{{totalPrice}}</td>
                </tr>
            </tbody>
        </table>
    </div>
    <script>
        let vm = new Vue({
            el: '#app',
            data: {
                cart: [
                    {name: 'apply', num: 20, price: 3},
                    {name: 'call', num: 14, price: 2},
                    {name: 'bind', num: 49, price: 9},
                ]
            },
            computed: {
                totalNum() {
                    let num = 0;
                    this.cart.forEach(item => {
                        num += item.num * 1
                    });
                    return num+'件'
                },
                totalPrice() {
                    let num = 0;
                    this.cart.forEach(item => {
                        num += item.num * item.price
                    });
                    return num.toFixed(2)+'元'
                }
            }
        })
    </script>
</body>
</html>